let stream = weex.requireModule('stream');
const host = 'http://api.m.mi.com/v1/';

export default class Stream {
    constructor(url) {
        this.url = `${host}${url}`;
    }
    getHeaders() {
        // return {
        //     "Uuid": '7485cab4-0bf5-33c7-9e43-7cc66b0ee89e',
        //     "device": 'hNjY6VhwEPoBrUEwY6DCVQ==',
        //     "Device-Id": 'ffffffff-f1bc-8069-a70c-bab96cbf634b',
        //     "Mishop-Client-Id": '180100031052',
        //     "Content-Type": 'application/x-www-form-urlencoded'
        // };
        return {
            "Device-Id": 'ffffffff-f1bc-8069-a70c-bab96cbf634b',
            // "Cookie": 'serviceToken=bOYOYHtsnxHuBHXQ+q3MfLs1/jEJvmRdXQHl/4e2TUPfWN5iWjcr32ig9gdu+z5+SyssXEpti3VqjTnpthPmZkAXRSq+uL5HyXemthpOvF/TV/oN9m13qA0aZipIp8p9mjitXiSCoEBsfzKqLB1zn9QFhAeCkuxnyaIVsoiz+aY=;xmuuid=XMGUEST-ef75f547-7355-4fde-bd1f-97676c84931e;',
            "Network-Stat": 'wifi',
            "city": '5bm/5bee5biC',
            "Mishop-Client-Id": '180100031052',
            "Screen-width-px": '1080',
            "Mishop-Client-VersionName": '4.2.8.0907.r1',
            // "Accept-Encoding": 'gzip',
            "Mishop-Client-VersionCode": '20170907',
            "Mishop-Auth": '35f39bbccd01c853;3686029707',
            "Mishop-Model": 'MI 5',
            "Uuid": 'fa1b6c04-0f19-469a-f81b-f9f8095dc16c',
            "Screen-DensityDpi": '480',
            "Mishop-Is-Pad": '0',
            "device": 'hNjY6VhwEPoBrUEwY6DCVQ==',
            "Android-Ver": '24',
            "If-None-Match": '789bb31a9867889b6a3b92166e8dc87d',
            "Content-Type": 'application/x-www-form-urlencoded; charset=UTF-8'
        };
    }
    get(params) {
        console.log(this.url);
        return new Promise((resolve, reject) => {
            stream.fetch({
                method: 'GET',
                type: 'json',
                headers: this.getHeaders(),
                url: this.url
            }, (res) => {
                let data = res.data;
                if (typeof data === 'string') {
                    data = JSON.parse(data);
                }
                if (res.status === 200) {
                    resolve(data);
                } else {
                    reject(res.statusText);
                }
            });
        });
    }
    post(data) {
        return new Promise((resolve, reject) => {
            stream.fetch({
                method: 'POST',
                type: 'json',
                body: JSON.stringify(data),
                headers: this.getHeaders(),
                url: this.url
            }, (res) => {
                let data = res.data;
                if (typeof data === 'string') {
                    data = JSON.parse(data);
                }
                if (res.status === 200) {
                    resolve(data);
                } else {
                    reject(res.statusText);
                }
            });
        });
    }
}